<template>
    <div class="review-container">
      <h1>用户评价</h1>
  
      <!-- Content for the selected tab -->
      <div v-if="activeTab === 'reviewed'" class="content">
        <p class="subtitle">已评价的宝贝展示区</p>
        <ul class="comments-list">
          <li v-for="comment in orderComments" :key="comment.comm_id" class="comment-item">
            <div class="comment">
              <p class="rating">评价等级: {{ getRatingText(comment.grade) }}</p>
              <p class="comment-text">评价内容: {{ comment.comment }}</p>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </template>
  
  <script setup>
  import axios from 'axios';
  import { onMounted, ref } from 'vue';
  
  // Define a reactive variable to store the user ID
  const user = ref('1');
  
  // Track the active tab
  const activeTab = ref('reviewed'); // Default to '已评价'
  
  // Define a reactive variable to store the order comments
  const orderComments = ref([]);
  
  // Function to get the rating text based on the grade
  const getRatingText = (grade) => {
    const ratings = {
      '1': '好评',
      '2': '中评',
      '3': '差评',
      // Add more ratings as needed
    };
    return ratings[grade] || '无评级';
  };
  
  // Function to fetch and update the order comments
  const changeTab = async () => {
    try {
      const response = await axios.get(`http://127.0.0.1:8000/order/usercommquy/?user=${user.value}`);
      if (response.data.code == 200) {
        orderComments.value = response.data.data; // Assign the fetched comments to orderComments
      }
    } catch (err) {
      console.error(err);
    }
  };
  
  // Call changeTab when the component is mounted
  onMounted(() => {
    changeTab();
  });
  </script>
  
  <style scoped>
/* General styles */
body {
  font-family: 'Arial', sans-serif;
  background-color: #f4f4f9;
  margin: 0;
  padding: 0;
}

/* Review container styles */
.review-container {
  max-width: 800px;
  margin: 50px auto;
  padding: 20px;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* Heading styles */
h1 {
  font-size: 2em;
  color: #333;
  margin-bottom: 20px;
  text-align: center;
}

/* Subtitle styles */
.subtitle {
  font-size: 1.2em;
  color: #666;
  margin-bottom: 10px;
  text-align: center;
}

/* Comments list styles */
.comments-list {
  list-style: none;
  padding: 0;
}

/* Comment item styles */
.comment-item {
  border-bottom: 1px solid #eaeaea;
  padding: 15px 0;
}

/* Comment rating styles */
.rating {
  font-size: 1.1em;
  color: #333;
  margin-bottom: 5px;
}

/* Comment text styles */
.comment-text {
  font-size: 1em;
  color: #555;
  line-height: 1.6;
}

/* Responsive styles */
@media (max-width: 768px) {
  .review-container {
    padding: 15px;
    margin: 30px auto;
  }

  h1 {
    font-size: 1.5em;
  }

  .subtitle {
    font-size: 1em;
  }

  .comment-item {
    padding: 10px 0;
  }
}
</style>